<?php

namespace zhtfrontend\models\datas;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%report_record}}".
 *
 * @property integer $module_id
 * @property integer $sender_id
 * @property integer $sendee_id
 * @property integer $stage
 * @property integer $type
 * @property integer $status
 * @property string $mechanicremark
 * @property string $workerremark
 * @property integer $created_at
 */
class ReportRecord extends \zhtfrontend\components\bases\BaseModel
{
    public $remark;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%report_record}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['module_id', 'sender_id', 'sendee_id', 'stage', 'type', 'status', 'created_at','is_visual','updated_at'], 'integer'],
            [['operator_type'], 'string'],
            [['mechanicremark', 'workerremark','remark'], 'string', 'max' => 255],
            [['remark'], 'required'],
            ['status','required','on'=>['audit']]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'module_id' => Yii::t('zhtfrontend', '模块id'),
            'sender_id' => Yii::t('zhtfrontend', '提报者id'),
            'sendee_id' => Yii::t('zhtfrontend', '接收者id'),
            'stage' => Yii::t('zhtfrontend', '环节id'),
            'type' => Yii::t('zhtfrontend', '类型id（1 商品 2 结算）'),
            'status' => Yii::t('zhtfrontend', '审核状态'),
            'mechanicremark' => Yii::t('zhtfrontend', '机构备注'),
            'workerremark' => Yii::t('zhtfrontend', '广场工作人员'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'remark' => Yii::t('zhtfrontend', '备注'),
            'is_visual' => Yii::t('zhtfrontend', '(备注)机构是否可见'),
            'operator_type' => Yii::t('zhtfrontend', '提交人类型'),
            'updated_at' => Yii::t('zhtfrontend', '修改时间'),
        ];
    }
    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['updated_at'] = function () {
            return \Yii::$app->formatter->asDate($this->updated_at);
        };
        $fields['status'] = function (){
            switch ($this->status){
                case 1:
                    return Yii::t('zhtfrontend', '通过');
                case 2:
                    return Yii::t('zhtfrontend', '退回');
                default :
                    return Yii::t('zhtfrontend', '提报');
            }
        };
        return $fields;
    }

    public function extraFields(){
        return [
            'sender_name' => function(){
                  return isset($this->sender->name)?$this->sender->name:'';
            },
            'sendee_name' => function(){
                  return isset($this->sendee->name)?$this->sendee->name:'';
            },
            'remark' => function(){
                 if($this->operator_type == 'square')
                    return $this->workerremark;
                 else
                    return $this->mechanicremark;
            }
        ];

    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }

    public function getSender(){
        return $this->hasOne(Admin::className(),['id'=>'sender_id']);
    }
    public function getSendee(){
        return $this->hasOne(Admin::className(),['id'=>'sendee_id']);
    }
}
